﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace hnrzxt.Controllers
{
    public class HmcWHController : Controller
    {
        private readonly ApplicationDbContext _db;

        public HmcWHController(ApplicationDbContext context)
        {
            _db = context;
        }
        public IActionResult Index()
        {
            return View();
        }

        public string Save_ldgx(hr_ldgx data)
        {
            try
            {
                _db.hr_ldgx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_ldgx(hr_ldgx data)
        {
            try
            {
                _db.hr_ldgx.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_ldgx(string jbxxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_ldgx> ldgxlist = _db.hr_ldgx.Where(p => p.jbxxid == jbxxid).ToList();

                for (int i = 0; i < ldgxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", ldgxlist[i].Id);
                    map.Add("jbxxid", ldgxlist[i].jbxxid);
                    map.Add("JobStatus", ldgxlist[i].JobStatus);
                    map.Add("LeaveDate", ldgxlist[i].LeaveDate.ToString());
                    map.Add("leaveNote", ldgxlist[i].leaveNote);
                    map.Add("WorkFiles", ldgxlist[i].WorkFiles);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Save_ldht(hr_ldht data)
        {
            try
            {
                _db.hr_ldht.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_ldht(hr_ldht data)
        {
            try
            {
                _db.hr_ldht.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public string Del_ldht(string id)
        {
            try
            {
                var ldht = _db.hr_ldht.Find(id);
                if (ldht == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.hr_ldht.Remove(ldht);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
        public IActionResult get_ldht(string jbxxid, string ldhtid, string type)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_ldht> ldhtlist = new List<hr_ldht>();

                if (ldhtid != null)
                {
                    ldhtlist = _db.hr_ldht.Where(p => p.Id == ldhtid).OrderByDescending(p => p.sdate).ToList();
                }
                else
                {
                    ldhtlist = _db.hr_ldht.Where(p => p.jbxxid == jbxxid).OrderByDescending(p => p.sdate).ToList();
                }

                int num = 0;
                int length = ldhtlist.Count;
                if (length < 1)
                {
                    return null;
                }

                if (type == "last")
                    length = 1;
                else
                    num = 1;

                for (int i = num; i < length; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", ldhtlist[i].Id);
                    map.Add("jbxxid", ldhtlist[i].jbxxid);
                    map.Add("type", ldhtlist[i].type);                    
                    map.Add("sdate", ldhtlist[i].sdate.ToString("yyyy-MM-dd"));
                    map.Add("edate", ldhtlist[i].edate.ToString("yyyy-MM-dd"));
                    map.Add("year", ldhtlist[i].year);
                    list.Add(map);
                }

                return Json(list);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string Save_syq(hr_syq data)
        {
            try
            {
                _db.hr_syq.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_syq(hr_syq data)
        {
            try
            {
                _db.hr_syq.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_syq(string jbxxid, string syqid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_syq> syqlist = new List<hr_syq>();

                if (syqid != null)
                {
                    syqlist = _db.hr_syq.Where(p => p.Id == syqid).ToList();
                }
                else
                {
                    syqlist = _db.hr_syq.Where(p => p.jbxxid == jbxxid).ToList();
                }


                for (int i = 0; i < syqlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", syqlist[i].Id);
                    map.Add("jbxxid", syqlist[i].jbxxid);
                    map.Add("syq", syqlist[i].syq);
                    map.Add("sdate", syqlist[i].sdate.ToString("yyyy-MM-dd"));
                    map.Add("edate", syqlist[i].edate.ToString("yyyy-MM-dd"));
                    map.Add("zzlx", syqlist[i].zzlx);
                    map.Add("zznote", syqlist[i].zznote);
                    map.Add("isjf", syqlist[i].isjf);
                    map.Add("jfdate", syqlist[i].jfdate);
                    map.Add("jfr", syqlist[i].jfr);
                    map.Add("isdb", syqlist[i].isdb);
                    map.Add("dbr", syqlist[i].dbr);
                    map.Add("dbrgx", syqlist[i].dbrgx);
                    map.Add("dbfiles", syqlist[i].dbfiles);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Save_pzxx(hr_pzxx data)
        {
            try
            {
                _db.hr_pzxx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_pzxx(hr_pzxx data)
        {
            try
            {
                _db.hr_pzxx.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_pzxx(string jbxxid, string pzxxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_pzxx> pzxxlist = new List<hr_pzxx>();

                if (pzxxid != null)
                {
                    pzxxlist = _db.hr_pzxx.Where(p => p.Id == pzxxid).OrderByDescending(p=>p.pzdate).ToList();
                }
                else
                {
                    pzxxlist = _db.hr_pzxx.Where(p => p.jbxxid == jbxxid).OrderByDescending(p => p.pzdate).ToList();
                }


                for (int i = 0; i < pzxxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", pzxxlist[i].Id);
                    map.Add("jbxxid", pzxxlist[i].jbxxid);
                    map.Add("pzdate", pzxxlist[i].pzdate);
                    map.Add("pztype", pzxxlist[i].pztype); 
                    map.Add("pzgs", pzxxlist[i].pzgs);
                    map.Add("pzbm", pzxxlist[i].pzbm);
                    map.Add("pzgsname",GetOrgInfo(pzxxlist[i].pzgs));
                    map.Add("pzgw", pzxxlist[i].pzgw);
                    map.Add("xzjb", pzxxlist[i].xzjb);
                    map.Add("pznote", pzxxlist[i].pznote);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Del_pzxx(string id)
        {
            try
            {
                var pzxx = _db.hr_pzxx.Find(id);
                if (pzxx == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.hr_pzxx.Remove(pzxx);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
        public string Save_rzpx(hr_rzpx data)
        {
            try
            {
                _db.hr_rzpx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_rzpx(hr_rzpx data)
        {
            try
            {
                _db.hr_rzpx.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_rzpx(string jbxxid, string rzpxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_rzpx> rzpxlist = new List<hr_rzpx>();

                if (rzpxid != null)
                {
                    rzpxlist = _db.hr_rzpx.Where(p => p.Id == rzpxid).ToList();
                }
                else
                {
                    rzpxlist = _db.hr_rzpx.Where(p => p.jbxxid == jbxxid).ToList();
                }


                for (int i = 0; i < rzpxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", rzpxlist[i].Id);
                    map.Add("jbxxid", rzpxlist[i].jbxxid);
                    map.Add("rzpxr", rzpxlist[i].rzpxr);
                    map.Add("pxjh", rzpxlist[i].pxjh);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Save_qtpx(hr_qtpx data)
        {
            try
            {
                _db.hr_qtpx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_qtpx(hr_qtpx data)
        {
            try
            {
                _db.hr_qtpx.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_qtpx(string jbxxid, string qtpxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_qtpx> qtpxlist = new List<hr_qtpx>();

                if (qtpxid != null)
                {
                    qtpxlist = _db.hr_qtpx.Where(p => p.Id == qtpxid).ToList();
                }
                else
                {
                    qtpxlist = _db.hr_qtpx.Where(p => p.jbxxid == jbxxid).ToList();
                }


                for (int i = 0; i < qtpxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", qtpxlist[i].Id);
                    map.Add("jbxxid", qtpxlist[i].jbxxid);
                    map.Add("pxdate", qtpxlist[i].pxdate.ToString("yyyy-MM-dd"));
                    map.Add("pxname", qtpxlist[i].pxname);
                    map.Add("pxr", qtpxlist[i].pxr);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Del_qtpx(string id)
        {
            try
            {
                var qtpx = _db.hr_qtpx.Find(id);
                if (qtpx == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.hr_qtpx.Remove(qtpx);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
        public string Save_wbpx(hr_wbpx data)
        {
            try
            {
                _db.hr_wbpx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_wbpx(hr_wbpx data)
        {
            try
            {
                _db.hr_wbpx.Update(data);
                _db.SaveChanges();
                return "修改成功！";
            }
            catch (Exception e)
            {
                return "修改失败:" + e.Message;
            }
        }
        public IActionResult get_wbpx(string jbxxid, string wbpxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_wbpx> wbpxlist = new List<hr_wbpx>();

                if (wbpxid != null)
                {
                    wbpxlist = _db.hr_wbpx.Where(p => p.id == wbpxid).ToList();
                }
                else
                {
                    wbpxlist = _db.hr_wbpx.Where(p => p.jbxxid == jbxxid).ToList();
                }

                for (int i = 0; i < wbpxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", wbpxlist[i].id);
                    map.Add("jbxxid", wbpxlist[i].jbxxid);
                    map.Add("pxdate", wbpxlist[i].pxdate.ToString("yyyy-MM-dd"));
                    map.Add("pxname", wbpxlist[i].pxname);
                    map.Add("pxr", wbpxlist[i].pxr);
                    map.Add("pxfy", wbpxlist[i].pxfy);
                    map.Add("pxgs", wbpxlist[i].pxgs);
                    map.Add("pxqx", wbpxlist[i].pxqx);
                    map.Add("pxxy", wbpxlist[i].pxxy);
                    map.Add("isxy", wbpxlist[i].isxy);
                    map.Add("note", wbpxlist[i].note);

                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Del_wbpx(string id)
        {
            try
            {
                var wbpx = _db.hr_wbpx.Find(id);
                if (wbpx == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.hr_wbpx.Remove(wbpx);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
        public string Save_zsxx(hr_zsxx data)
        {
            try
            {
                _db.hr_zsxx.Add(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public string Update_zsxx(hr_zsxx data)
        {
            try
            {
                _db.hr_zsxx.Update(data);
                _db.SaveChanges();
                return "保存成功！";
            }
            catch (Exception e)
            {
                return "保存失败:" + e.Message;
            }
        }
        public IActionResult get_zsxx(string jbxxid, string zsxxid)
        {
            try
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                List<hr_zsxx> zsxxlist = new List<hr_zsxx>();

                if (zsxxid != null)
                {
                    zsxxlist = _db.hr_zsxx.Where(p => p.Id == zsxxid).ToList();
                }
                else
                {
                    zsxxlist = _db.hr_zsxx.Where(p => p.jbxxid == jbxxid).ToList();
                }

                for (int i = 0; i < zsxxlist.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", zsxxlist[i].Id);
                    map.Add("jbxxid", zsxxlist[i].jbxxid);
                    map.Add("zsname", zsxxlist[i].zsname);
                    map.Add("getdate", zsxxlist[i].getdate.ToString("yyyy-MM-dd"));
                    map.Add("zsfj", zsxxlist[i].zsfj);
                    list.Add(map);
                }

                return Json(list);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        public string Del_zsxx(string id)
        {
            try
            {
                var zsxx = _db.hr_zsxx.Find(id);
                if (zsxx == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.hr_zsxx.Remove(zsxx);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
        public IActionResult get_OrgUsers()
        {
            try
            {
                List<Dictionary<string, object>> orguserlist = new List<Dictionary<string, object>>();
                List<hr_jbxx> orgusers = _db.hr_jbxx.OrderBy(p => p.name).ToList();

                for (int i = 0; i < orgusers.Count; i++)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("title", orgusers[i].name);
                    orguserlist.Add(map);
                }

                return Json(orguserlist);

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string GetOrgInfo(string OrgId)
        {
            //从数据库获取菜单数据
            List<Org> orglist = _db.Orgs.Where(p => p.OrgId == int.Parse(OrgId)).ToList();
            if (orglist.Count < 1)
                return OrgId;
            else
                return orglist[0].OrgName;
        }

        public IActionResult GetOrgTreeData()
        {
            return new JsonResult(GetOrgTree("0")); //0从华南集团开始，4从华南集团下级开始
        }


        private List<Dictionary<string, object>> GetOrgTree(string ParentOrgId)
        {
            if (int.Parse(ParentOrgId) > 4)
            {
                return null;
            }

            List<Org> orglist = _db.Orgs.Where(p => p.ParentOrgId == ParentOrgId).ToList();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            if (orglist.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < orglist.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", orglist[i].OrgId);
                map.Add("title", orglist[i].OrgName);
                //map.Add("name", orglist[i].OrgName);
                //map.Add("dataid", orglist[i].OrgId);


                List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                listchildren = GetOrgTree(orglist[i].OrgId.ToString());
                if (listchildren != null)
                {
                    map.Add("child", listchildren);
                }
                list.Add(map);
            }
            return list;
        }

    }
}
